import 'package:OASys/api/userApi.dart';
import 'package:OASys/components/MyLoading.dart';
import 'package:OASys/entity/UserVo.dart';
import 'package:OASys/views/icon/myIcons.dart';
import 'package:flutter/material.dart';
import 'package:toast/toast.dart';

class PersonalPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MyPersonalPage();
  }
}

class MyPersonalPage extends StatefulWidget {
  @override
  _MyPersonalPageState createState() => _MyPersonalPageState();
}

class _MyPersonalPageState extends State<MyPersonalPage> {
  UserVo userVo;
  void getInfo() {
    UserApi().info(success: (res) {
      setState(() {
        userVo = res;
      });
    });
  }

  @override
  void initState() {
    super.initState();
    getInfo();
  }

  void toEdit() {
    print('---');
    print(userVo.toJson());
    print('---');
    Navigator.of(context)
        .pushNamed("/personalInfoEdit", arguments: userVo.toJson());
    Toast.show('编辑信息', context, gravity: Toast.CENTER);
  }

  @override
  Widget build(BuildContext context) {
    if (userVo == null) {
      return MyLoading(
        loadingText: '加载中',
      );
    } else {
      return Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              height: 30,
            ),
            AvatarItem(
              userVo.username,
              toEdit,
              avatar: userVo.avatar,
            ),
            IfonItem('姓名', userVo.nickname == null ? "无" : userVo.nickname),
            IfonItem('性别', '男'),
            IfonItem('部门',
                userVo.personnelName == null ? "无" : userVo.personnelName),
            IfonItem('团队',
                userVo.personnelName == null ? "无" : userVo.personnelName),
            IfonItem('小组', '百量养猪小组'),
            IfonItem('职位', 'Java攻城狮'),
            IfonItem('邮箱', userVo.phone == null ? "无" : userVo.phone),
            IfonItem('电话', userVo.email == null ? "无" : userVo.email),
          ],
        ),
        decoration: BoxDecoration(color: Color.fromRGBO(230, 230, 230, 1)),
      );
    }
  }
}

class AvatarItem extends StatelessWidget {
  final String avatar;

  final String username;

  final Function toEdit;
  const AvatarItem(
    this.username,
    this.toEdit, {
    this.avatar,
    Key key,
  });

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Row(
        children: [
          SizedBox(width: 50, height: 88),
          Container(
            width: 50,
            height: 50,
            decoration: BoxDecoration(
              image: buildDecorationImage(avatar),
              border: Border.all(
                color: Colors.blue,
                width: 2,
              ),
              shape: BoxShape.circle,
            ),
          ),
          SizedBox(
            width: 30,
          ),
          Text(
            "账号",
            style: TextStyle(fontSize: 18),
          ),
          Text("："),
          SizedBox(width: 10),
          Text(
            username,
            style: TextStyle(fontSize: 18),
          ),
          Expanded(
            child: IconButton(
              icon: Icon(
                MyIcons.edit,
                color: Color.fromRGBO(133, 133, 133, 1),
                size: 15,
              ),
              onPressed: toEdit,
            ),
          ),
        ],
      ),
    );
  }

  DecorationImage buildDecorationImage(String avatar) {
    if (avatar == null) {
      return null;
    }
    return DecorationImage(
      image: NetworkImage(avatar),
      fit: BoxFit.cover,
    );
  }
}

class IfonItem extends StatelessWidget {
  final String prefix;

  final String data;

  const IfonItem(
    this.prefix,
    this.data, {
    Key key,
  });

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Row(
        children: [
          SizedBox(
            width: 50,
            height: 50,
          ),
          Text(
            prefix,
            style: TextStyle(fontSize: 15),
          ),
          Text('：'),
          SizedBox(
            width: 30,
          ),
          Text(
            data,
            style: TextStyle(
                color: Color.fromRGBO(133, 133, 133, 1), fontSize: 15),
          ),
        ],
      ),
    );
  }
}
